﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using AlexandriaPressAuthorBusinessLayer;
using AlexandriaPressAuthorObject;

namespace AlexandriaPressWebsite.MasterPages.AuthorPages
{
    public partial class DraftDetails : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            BusinessAuthor bus = new BusinessAuthor();

            if (!Page.IsPostBack)
            {
                if (HttpContext.Current.User.Identity.IsAuthenticated)
                {
                    if (bus.validateUserIfAuthor(bus.getAuthorID(HttpContext.Current.User.Identity.Name)))
                    {
                        Draft draft = new Draft();

                        if (Session["draftID"] != null)
                            draft = (Draft)Session["draftID"];
                        else
                            Session["draftID"] = Convert.ToInt32(Request.QueryString["id"]);
                        
                        draft = bus.viewDraft((int)Session["draftID"], bus.getAuthorID(HttpContext.Current.User.Identity.Name));

                        lblGenre.Text = draft.Genre;
                        lblTitle.Text = draft.WorkingTitle;
                        lblComments.Text = draft.Comments;
                        lblStatus.Text = draft.ApprovalStatus;
                        Image1.ImageUrl = draft.BookCoverLocation;

                        txtGenre.Visible = false;
                        txtTitle.Visible = false;
                        Button1.Visible = false; ;
                        Session["Draft"] = draft;
                        Label5.Visible = false;
                        Label6.Visible = false;
                        uploadFile.Visible = false;
                        uploadImage.Visible = false;
                    
                    }
                    else
                    {
                        Response.Redirect("~/MasterPages/UserPages/LoggedInUserPage.aspx");
                    }

                }
                else
                {
                    Response.Redirect("~/MasterPages/MainPage.aspx");
                }
                
            }

            
            
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            BusinessAuthor bus = new BusinessAuthor();

            Draft draft = (Draft)Session["Draft"];
            lblEditStatus.Text = bus.editDraft(draft.DraftID, txtTitle.Text, txtGenre.Text, bus.getAuthorID(HttpContext.Current.User.Identity.Name), uploadFile.FileName.Replace(" ", ""), Server.MapPath(bus.filePath() + uploadFile.FileName.Replace(" ", "")), Server.MapPath(bus.imagePath() + uploadImage.FileName), uploadFile.PostedFile, uploadImage.PostedFile, uploadImage.FileName, draft.FileLocation, draft.BookCoverLocation);
            txtTitle.Visible = false;
            txtGenre.Visible = false;

            lblGenre.Visible = true;
            lblTitle.Visible = true;

            lblGenre.Text = draft.Genre;
            lblTitle.Text = draft.WorkingTitle;
            Label5.Visible =false;
            Label6.Visible = false;
            uploadFile.Visible = false;
            uploadImage.Visible = false;
        }

        protected void butEdit_Click(object sender, EventArgs e)
        {
            txtGenre.Visible = true;
            txtTitle.Visible = true;
            Button1.Visible = true;


            Draft draft = (Draft)Session["Draft"];
            txtGenre.Text = draft.Genre;
            txtTitle.Text = draft.WorkingTitle;

            lblComments.Text = draft.Comments;
            lblStatus.Text = draft.ApprovalStatus;
            lblGenre.Visible = false;
            lblTitle.Visible = false;
            Label5.Visible = true;
            Label6.Visible = true;
            uploadFile.Visible = true;
            uploadImage.Visible = true;
        }

        protected void TextBox1_TextChanged(object sender, EventArgs e)
        {

            BusinessAuthor business = new BusinessAuthor();

            if (lblTitle.Text != txtTitle.Text)
                lblTitleCheck.Text = business.checkDraftTitle(txtTitle.Text, business.getAuthorID(HttpContext.Current.User.Identity.Name));

            lblGenre.Visible = false;
            lblTitle.Visible = false;
            Label5.Visible = true;
            Label6.Visible = true;
            uploadFile.Visible = true;
            uploadImage.Visible = true;
        }

    }
}
